package cn.zsb.studentSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        System.out.println("--------欢迎来到学生管理系统--------");
        loop : while (true) {
            System.out.println("\n1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入宁德选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch(choose) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    break loop;
                    //System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    public static void addStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        Student student = new Student();
        String id;

        System.out.println("请输入学生id：");
        while(true){
            id = sc.next();
            boolean flag = contains(list,id);
            if (flag){
                System.out.println("已存在！请重新输入");
            }else {
                student.setId(id);
                break;
            }
        }
        System.out.println("请输入姓名：");
        String name = sc.next();
        student.setName(name);

        System.out.println("请输入年龄：");
        int age = sc.nextInt();
        student.setAge(age);

        System.out.println("请输入家庭地址：");
        String address = sc.next();
        student.setAddress(address);

        list.add(student);
        System.out.println("添加成功");
    }

    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生id：");
        String id = sc.next();
        int index = getIndex(list,id);
        if (index >= 0) {
            list.remove(index);
            System.out.println("id为" + id + "的学生删除成功");
        } else {
            System.out.println("不存在，删除失败");
        }

    }

    public static void updateStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生id：");
        String id = sc.next();
        int index = getIndex(list,id);
        if (index == -1) {
            System.out.println("id为" + id + "的学生不存在，请重新输入");
            return;
        }

        Student stu = list.get(index);
        System.out.println("新姓名：");
        String name = sc.next();
        stu.setName(name);
        System.out.println("新年龄：");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("新地址：");
        String address = sc.next();
        stu.setAddress(address);
        System.out.println("修改成功");
    }

    public static void queryStudent(ArrayList<Student> list){
        if (list.isEmpty()){
            System.out.println("空空如也");
            return;
        }
        System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址");
        for (Student stu : list) {
            System.out.println(stu.getId() + "\t" + stu.getName()
                    + "\t" + stu.getAge() + "\t" + stu.getAddress());
        }
    }

    public static boolean contains (ArrayList<Student> list, String id) {
        /*for (Student stu : list) {
            String sid = stu.getId();
            if (sid.equals(id)) {
                return true;
            }
        }*/
        return getIndex(list,id) >= 0;
    }

    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if (sid.equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
